JingJiChangScene={}
JingJiChangSceneFunc={}
JingJiChangSceneFunc.__index = JingJiChangSceneFunc
setmetatable(JingJiChangScene, JingJiChangSceneFunc)
JingJiChangScene.NodeID = {UpButton=1020}
JingJiChangScene.upgradeCooldown = 0

JingJiChangSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, JingJiChangSceneFunc)
  l.node = CCBReader:load("JingJiChangScene.ccbi")
  print("JingJiChangScene.getNode", l.node)
  JingJiChangScene.instance = l
  l:init() 
  return l 
end

JingJiChangSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(JingJiChangScene.getNode().node)
  print("JingJiChangScene.getScene", sc)
  return sc
end

JingJiChangSceneFunc.init = function(self)
  b = self.node:getChildByTag(1111)
  setObjectExtTable(b, self)
  setObjectExtTable(self.node:getChildByTag(1020), self)
  setObjectExtTable(self.node:getChildByTag(1021), self)
  setObjectExtTable(self.node:getChildByTag(1022), self)
  setObjectExtTable(self.node:getChildByTag(1023), self)
  print("JingJiChangScene.init jingjiData", JingJiChangScene.jingjiData)
  if JingJiChangScene.jingjiData==nil then
    LoadScene:showLoadingScene()
    --setTouchEnabledAll(self.node, false)
    NetworkRequest.jingJiChang("onGetData", self)
  else
    self:setLabels(JingJiChangScene.jingjiData)
  end
end

JingJiChangSceneFunc.onGetData = function(self, data)
  print("JingJiChangSceneFunc.onGetData", data)
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
  
  local res = cjson.decode(data)
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
    return
  end
  JingJiChangScene.jingjiData = res
  print("JingJiChangScene.onGetData jingjiData", JingJiChangScene.jingjiData)
  self:setLabels(res)
end
JingJiChangSceneFunc.setLabels = function(self, res)
  TypeCast(self.node:getChildByTag(1016), "CCLabelTTF"):setString(Player.name)
  TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(tostring(res.sync.reputation))
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(tostring(res.data.userNewAthletics.rating))
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(tostring(res.data.userNewAthletics.winCount))
  TypeCast(self.node:getChildByTag(1013), "CCLabelTTF"):setString(tostring(res.data.userNewAthletics.rating))
  TypeCast(self.node:getChildByTag(1014), "CCLabelTTF"):setString(tostring(res.data.userNewAthletics.leftAttack))
  
  local tmp = string.format("排名%d的奖励: %d钞票 %d声望 发奖时间%s", 
                              res.data.userNewAthletics.rating, 
                              res.data.userNewAthletics.nextReward[1].num,
                              res.data.userNewAthletics.nextReward[2].num,
                              formatSecond(res.data.userNewAthletics.nextRewardTime-TimeManager.getServerTime()))
  TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setString(tmp)
 
  local cd = res.data.userNewAthletics.nextFightTime - TimeManager.getServerTime()
  --local tmp = UITimer.getUITimer(string.format("upbuildingcd_%d", BuildingID.JingJiChang), self, ccp(166,72), ccp(0, 0))
  if cd>0 then
    self.uitimer = UITimer.createUITimer("jingjicd", cd, self, ccp(-15,15), ccp(0, 0))
    self.uitimer.view:setPosition(ccp(750, 166))
    self.node:addChild(self.uitimer.view)
  end
  --[[
  JingJiChangScene.fightCD = cd
  self:setFightCoolDown()
  if cd>0 and JingJiChangScene.timerID==nil then
    JingJiChangScene.timerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(JingJiChangSceneFunc.onTimer, 1, false)
  end
  ]]
  self.loginData = res 
  local attackList = res.data.userNewAthletics.attackList;
  for k,v in ipairs(attackList) do
    local item = self.node:getChildByTag(2000+k)
   
    local imgName = nameFromPath(v.headPic)--string.format("%d.png",k%40) 
    local imgBK = item:getChildByTag(1)
    local button = CCButton:createWithFileName("onClickHero", imgName, imgName, imgName, "")
    item:addChild(button)
    setObjectExtTable(button, self)
    button:setTag(k)
    button:setPosition(imgBK:getPosition())
 
    local lb1 = item:getChildByTag(2)
    local lb2 = CCLabelTTF:create(string.format("%d", v.rating), "Helvetica", 28)
    lb2:setPosition(lb1:getPosition())
    local sp = CCSprite:create("arena_bk1.png")
    sp:setPosition(lb1:getPosition())
    item:addChild(sp)
    item:addChild(lb2)
    item:removeChild(lb1, true)
    --TypeCast(item:getChildByTag(2), "CCLabelTTF"):setString(string.format("%d",v.rating))
    TypeCast(item:getChildByTag(3), "CCLabelTTF"):setString(stringLimit(v.name, 12))
    TypeCast(item:getChildByTag(4), "CCLabelTTF"):setString(string.format("Lv.%d",v.level))
  end
end
--[[
JingJiChangSceneFunc.setFightCoolDown= function(self)
  --local lb = self.node:getChildByTag(1007)
  --local b = self.node:getChildByTag(ZhiHuiBuScene.NodeID.ShouShuiButton)
  if JingJiChangScene.fightCD <=0 then
    TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setVisible(false)
    TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false)
  else
    TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setVisible(true)
    TypeCast(self.node:getChildByTag(1015), "CCLabelTTF"):setString(formatSecond(JingJiChangScene.fightCD))
    if self.node:isTouchEnabled()==true then
      TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(true)
    end
  end
end]]
--[[
JingJiChangSceneFunc.onTimer = function()
  JingJiChangScene.fightCD = JingJiChangScene.fightCD-1
  if JingJiChangScene.instance~=nil then
    JingJiChangScene.instance:setFightCoolDown()
  end
  if JingJiChangScene.fightCD<=0 then
    print("onTimer stop timer")
    CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(JingJiChangScene.timerID)
  end
end
]]
JingJiChangSceneFunc.onClickHero = function(self, node, event)
  --NetworkRequest.jingJiChang("onGetData", self) 
  if UserData.userinfo.host.soldiers>0 then
    local attackList = self.loginData.data.userNewAthletics.attackList;
    self.currentIndex = node:getTag()
    local tmp = attackList[self.currentIndex]
    --setTouchEnabledAll(self.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.getJingJiChangBattle(tmp.uid, tmp.rating, "onBattleComplete", self)
  else
    showMessage("兵力不足，请补充兵力！", ccc3(255,255,0))
  end
end

JingJiChangSceneFunc.onBattleComplete = function(self, data)
  print("JingJiChangSceneFunc.onBattleComplete", data)
  local res = cjson.decode(data)
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
    setTouchEnabledAll(self.node, true)
  else
    NetworkRequest.syncData(res)
    self.node:getParent():removeChild(self.node, true)
    local attackList = self.loginData.data.userNewAthletics.attackList;
    local tmp = attackList[self.currentIndex]
    local war = WarScene:getNode(tmp.name, self, res)
    MainScene.scene:addChild(war.node)
  end
  LoadScene:removeLoadingScene()
end
JingJiChangSceneFunc.fightEnd = function(self)
  JingJiChangScene.jingjiData = nil
  MainScene.scene:addChild(JingJiChangScene.getNode().node)
end
--[[
JingJiChangSceneFunc.onClearCDButton = function(self, node, event)
  JiaSuDialog:createExt("JingJiChang", JingJiChangScene.fightCD, self)
end
]]

JingJiChangSceneFunc.onGetRewardButton = function(self, node, event)
  
  local lastReward = self.loginData.data.userNewAthletics.lastReward
  if lastReward[1]~=nil and lastReward[2]~=nil then
    setTouchEnabledAll(self.node, false)
    local l = CCBReader:load("JingJiChangRewardDialog.ccbi")
    setObjectExtTable(l:getChildByTag(1111), self)
    setObjectExtTable(l:getChildByTag(1020), self)
    TypeCast(l:getChildByTag(1010), "CCLabelTTF"):setString(self.loginData.data.userNewAthletics.lastRating)
    TypeCast(l:getChildByTag(1012), "CCLabelTTF"):setString(tostring(self.loginData.data.userNewAthletics.lastReward[1].num))
    TypeCast(l:getChildByTag(1013), "CCLabelTTF"):setString(tostring(self.loginData.data.userNewAthletics.lastReward[2].num))
    self.node:addChild(l)
  else
    showMessage("没有可以领取的奖励", ccc3(255,255,0))
  end

end
JingJiChangSceneFunc.onRewardButton = function(self, node, event)
  NetworkRequest.getJingJiChangReward("onGetRewardComplete", self)
  node:getParent():getParent():removeChild(node:getParent(), true)
  --setTouchEnabledAll(self.node, true)
end
JingJiChangSceneFunc.onGetRewardComplete = function(self, data)
  print("JingJiChangSceneFunc.onGetRewardComplete", data)
  local res = cjson.decode(data)
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
  else
    showMessage("领取成功", ccc3(255,255,0))
  end
  setTouchEnabledAll(self.node, true)
end
JingJiChangSceneFunc.onAddCountButton = function(self, node, event)
  self.dlg = Dialog:create("提示", "你确定要花费2军饷增加一次挑战机会吗？", self)
  MainScene.scene:addChild(self.dlg.node)
  --MainScene.scene:removeChild(self.node, true)
  self:closeScene()
end

JingJiChangSceneFunc.onConfirm = function(self, dlg, confirm)
  if confirm==false then
    MainScene.scene:removeChild(dlg.node, true)
    MainScene.scene:addChild(JingJiChangScene:getNode().node)
  else
    --setTouchEnabledAll(dlg.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.addFightTimes("onAddCountComplete", self)
  end
end
JingJiChangSceneFunc.onAddCountComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  if self.dlg~=nil then
    MainScene.scene:removeChild(self.dlg.node, true)
  end
  JingJiChangScene.jingjiData = nil
  MainScene.scene:addChild(JingJiChangScene.getNode().node)
end


JingJiChangSceneFunc.onRankButton = function(self, node, event)
  NetworkRequest.getJingJiChangRank("onGetRankComplete", self)
  LoadScene:showLoadingScene()
  setTouchEnabledAll(self.node, false)
end
JingJiChangSceneFunc.onGetRankComplete = function(self, data)
  print("JingJiChangSceneFunc.onGetRankComplete", data)
  LoadScene:removeLoadingScene() 
  local res = cjson.decode(data)
  if res.ret~=0 then
    showMessage(res.msg, ccc3(255,255,0))
    setTouchEnabledAll(self.node, true)
  else
    self:showRankWnd(res)
  end
end
JingJiChangSceneFunc.showRankWnd = function(self, res)
  local l = CCBReader:load("JingJiChangRankScene.ccbi")
  setObjectExtTable(l:getChildByTag(1111), self)
  local table = self:createRankTable(res)
  l:addChild(table)
  self.node:addChild(l)
end
JingJiChangSceneFunc.createRankTable = function(self, res)
  local items = CCArray:create() 
  for i=1, #res.data.top20Users do
  --for k,v in ipairs(res.data.top20Users) do
    local v = res.data.top20Users[#res.data.top20Users-i+1]
    local item = CCLayer:create()
    addLabel(item, ccp(50,25), tostring(v.rating), "Helvetica", 32)
    addLabel(item, ccp(280,25), tostring(v.name), "Helvetica", 32)
    addLabel(item, ccp(524,25), tostring(v.level), "Helvetica", 32)
    addLabel(item, ccp(752,25), tostring(v.fightingPower), "Helvetica", 32)
    item:setContentSize(CCSizeMake(850, 50)) 
    items:addObject(item)
  end
  local tableview = CCTableView:create(1, CCSizeMake(850, 400), items)
  tableview:setPosition(ccp(50, 50))
  --self.node:addChild(tableview)
  return tableview
end

--处理加速
JingJiChangSceneFunc.onTimerEnd = function(self, timer)
  if timer~=nil then
    timer.view:getParent():removeChild(timer.view, true)
  end
end

JingJiChangSceneFunc.onJiaSuButton = function(self, timer, pars)
  local tmp = JiaSuSampleDialog:create(pars.leftTime, self)
  MainScene.scene:addChild(tmp.view)
  UITimer.removeUITimer(self.uitimer)
  MainScene.scene:removeChild(self.node, true)
  self.jiasuDlg = tmp
end

JingJiChangSceneFunc.onJiaSuConfirm = function(self, dlg, confirm, timereel, timeLeft)
  if confirm~=true then
    MainScene.scene:removeChild(dlg.view, true)
    MainScene.scene:addChild(JingJiChangScene.getNode().node)
  elseif dlg==self.jiasuDlg then
    NetworkRequest.clearJingJiChangCD(timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene()
  end
end
JingJiChangSceneFunc.onJiaSuComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    self.uitimer:endTimer()
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  MainScene.scene:removeChild(self.jiasuDlg.view, true)
  JingJiChangScene.jingjiData = nil
  MainScene.scene:addChild(JingJiChangScene.getNode().node)
end
------------
JingJiChangSceneFunc.onCloseRankWnd = function(self, node, event)
  node:getParent():getParent():removeChild(node:getParent(), true)
  setTouchEnabledAll(self.node, true)
end
JingJiChangSceneFunc.onCloseRewardWnd = function(self, node, event)
  node:getParent():getParent():removeChild(node:getParent(), true)
  setTouchEnabledAll(self.node, true)
end
JingJiChangSceneFunc.closeScene = function(self)
  UITimer.removeUITimer(self.uitimer)
  MainScene.scene:removeChild(self.node, true)
  JingJiChangScene.instance = nil
end
JingJiChangSceneFunc.onClose = function(self, node, event)
  print("JingJiChangSceneFunc.onClose")
  self:closeScene()
  JingJiChangScene.jingjiData = nil
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
end
